package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.Supplier;
import com.ws.response.SupplierListResponse;
import com.ws.response.SupplierPageResponse;
import com.ws.response.SupplierResponse;
import com.ws.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 11:12
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    /**
     * 供应商查询接口
     * @param pageable
     * @param supplierName
     * @return
     */
    @GetMapping("")
    public ResponseEntity<SupplierPageResponse> getSupplierPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="supplierName",required=false) String supplierName,
            @RequestParam(value="statusCd",required=false) String statusCd

    ){
        PaasPage<Supplier> supplierPaasPage = supplierService.getSupplierPage(pageable,supplierName,statusCd);
        return ResponseEntity.ok(new SupplierPageResponse(supplierPaasPage));
    }

    /**
     * 供应商查询接口
     * @param supplierName
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<SupplierListResponse> getSupplierList(
            @RequestParam(value="supplierName",required=false) String supplierName,
            @RequestParam(value="statusCd",required=false) String statusCd

    ){
        List<Supplier> supplierList = supplierService.getSupplierList(supplierName,statusCd);
        return ResponseEntity.ok(new SupplierListResponse(supplierList));
    }

    /**
     * 供应商新增接口
     * @param supplier
     * @return
     */
    @PostMapping("")
    public ResponseEntity<SupplierResponse> saveSupplier(@RequestBody @Valid Supplier supplier){
        Supplier supplier1 = supplierService.saveSupplier(supplier);
        return ResponseEntity.ok(new SupplierResponse(supplier1));
    }

    /**
     * 查询对应供应商的基本信息
     * @param supplierId
     * @return
     */
    @GetMapping("/{supplierId}/basicInfo")
    public ResponseEntity<SupplierResponse> getSupplierBasicInfo(@PathVariable Long supplierId){
        Supplier supplier = supplierService.getSupplierBasicInfo(supplierId);
        return ResponseEntity.ok(new SupplierResponse(supplier));
    }

    /**
     * 删除对应的供应商
     * @param supplierId
     * @return
     */
    @PostMapping("/d/{supplierId}")
    public ResponseEntity delSupplier(@PathVariable Long supplierId)  {
        supplierService.delSupplier(supplierId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的供应商
     * @param supplierId
     * @return
     */
    @PostMapping("/u/{supplierId}")
    public ResponseEntity<SupplierResponse> updateSupplier(@PathVariable Long supplierId,@RequestBody Supplier supplier)  {
        Supplier supplier1 = supplierService.updateSupplier(supplierId,supplier);
        return ResponseEntity.ok(new SupplierResponse(supplier1));
    }
}
